#include "wuMossaic.h"

wuMossaic::wuMossaic()
{

}

wuMossaic::~wuMossaic()
{
}

void wuMossaic::setPhysics(float _density, float _bounce, float _friction)
{
}

void wuMossaic::setup(ofxBox2d * _box2d, ofImage *_skin, ofRectangle rect, vector <ofPoint> *countourPts)
{
	wuBase2d::setup(_box2d, rect);
//	wuBaseSkin::setSkin(_skin, rect);
	// loop and add vertex points
	shape.clear();

	// loop and add vertex points
	for (int i=0; i<countourPts->size(); i++) {
        ofVec2f temp_pt;
        temp_pt.x = (*countourPts)[i].x;
        temp_pt.y = (*countourPts)[i].y;
        shape.addVertex(temp_pt.x , temp_pt.y);
	}

	triangles.clear();

	if(countourPts->size()) {

		// copy over the points into a polyline
		ofPolyline polyOutline;
		ofPolyline newPoly;

		// make sure to close the polyline and then
		// simplify and resample by spacing...
		shape.setClosed(true);
		shape.simplify();
		newPoly = shape.getResampledBySpacing(25);

		// save the outline...
		polyOutline = newPoly;

		// add some random points inside then
		// triangulate the polyline...
		addRandomPointsInside(newPoly, 255);
		vector <TriangleShape> tris = triangulatePolygonWithOutline(newPoly, polyOutline);

		shape.clear();

	// now loop through all the traingles and make a box2d triangle
        for (int i=0; i<tris.size(); i++) {
            ofxBox2dPolygon p;
            p.addTriangle(tris[i].a, tris[i].b, tris[i].c);
 //           p.setPhysics(1.0, 0.3, 0.3);
            p.setAsEdge(false);
            if(p.isGoodShape()) {

           		wuSolidRectangle c;
           		ofRectangle re;
          		re.x = tris[i].a.x;
          		re.y = tris[i].a.y;
          		re.width = 8;
          		re.height = 8;

                ofImage image_tmp;
                image_tmp.setFromPixels(_skin->getPixels(), _skin->getWidth(), _skin->getHeight(), OF_IMAGE_COLOR_ALPHA);
                image_tmp.crop(re.x-rect.x, re.y-rect.y, re.width, re.height);

                c.setup(_box2d, &image_tmp, re);
                triangles.push_back(c);
            }
        }
    }
	// done with shape clear it now
	shape.clear();

    bDesintegration=false;
}

void wuMossaic::update()
{
//    posSkin = poly.getCenter();
//	angSkin = ofDegToRad(poly.getRotation());
    for (int i=0; i<triangles.size(); i++) {
        triangles[i].update();
    }

	if(bDesintegration==true && triangles.size() > 0 && ofRandom(0, 3) < 1)
	{
		triangles.begin()->clear();
		triangles.erase(triangles.begin());
	}

}

void wuMossaic::draw(bool pinta_bg, int offX)
{
    for (int i=0; i<triangles.size(); i++) {
        triangles[i].draw(pinta_bg,offX);
    }
}

void wuMossaic::drawSelected(bool pinta_bg, int offX, ofRectangle _selection)
{
        for (int i = triangles.size()-1; i >= 0; i--)
        {
            bool condi = (triangles[i].getPosition().y > _selection.y) && (triangles[i].getPosition().y < _selection.y + _selection.height) &&
             (triangles[i].getPosition().x > _selection.x) && (triangles[i].getPosition().x < _selection.x + _selection.width);

            if (condi)
                triangles[i].draw(pinta_bg,offX);
        }
}

void wuMossaic::clear()
{
	shape.clear();
	for (int i=0; i<triangles.size(); i++) {
		triangles[i].clear();
	}
}

void wuMossaic::clear(ofRectangle _selection)
{
        for (int i = triangles.size()-1; i >= 0; i--)
        {
            bool condi = (triangles[i].getPosition().y > _selection.y) && (triangles[i].getPosition().y < _selection.y + _selection.height) &&
             (triangles[i].getPosition().x > _selection.x) && (triangles[i].getPosition().x < _selection.x + _selection.width);

            if (condi)
            {
                triangles[i].clear();
                triangles.erase(triangles.begin()+i);
            }
        }
}

void wuMossaic::touch(int _x,int _y, int forceX, int forceY)
{
	for (int i=0; i<triangles.size(); i++) {
		triangles[i].touch(_x,_y,forceX, forceY);
	}
}

void wuMossaic::desintegration(bool _des)
{
    bDesintegration = _des;
}

vector <ofPoint> wuMossaic::getPosition()
{
    vector <ofPoint> positions;

 	for (int i=0; i<triangles.size(); i++) {
       positions.push_back(triangles[i].getPosition());
 	}

    return positions;
}

void wuMossaic::setPosition(vector <ofPoint> positions)
{

    for (int i = 0; i < triangles.size(); i++)
        triangles[i].setPosition(positions[i]);
}

void wuMossaic::stop_gravity()
{

    for (int i = 0; i < triangles.size(); i++)
        triangles[i].stop_gravity();
}

